package com.bit.strongbaby;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Entity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class SelectActivity extends Activity {
	//TextView txt1;
	private String id;
	private String title;
	String line;
	ArrayList<BoardDTO> arr;
	BoardAdapter adapter; 
	ListView list;
	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select);
		Button btn1 = (Button) findViewById(R.id.sbutton1);
		btn1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent ini = new Intent(getBaseContext(), InsertActivity.class);
				startActivity(ini);
				finish();
			}
		});
		try {
			if(android.os.Build.VERSION.SDK_INT>9){
				StrictMode.ThreadPolicy polic = new StrictMode.ThreadPolicy.Builder()
												.permitAll().build();
				StrictMode.setThreadPolicy(polic);
			}


			String URL = "http://192.168.0.182:8080/ServerApp/JSONSelect.jsp";
			
			HttpClient client = new DefaultHttpClient();
			
			HttpParams params = client.getParams();
			HttpConnectionParams.setConnectionTimeout(params, 3000);
			HttpConnectionParams.setSoTimeout(params, 3000);

			HttpPost post = new HttpPost(URL);
			HttpResponse respost = client.execute(post);
			
			StringBuilder html = new StringBuilder();
			BufferedReader bufferder = new BufferedReader(
					new InputStreamReader(respost.getEntity().getContent()));
			String page = "";
			String line = bufferder.readLine();
			
			arr = new ArrayList<BoardDTO>();
			adapter = new BoardAdapter(this, R.layout.board, arr);
			list = (ListView)findViewById(R.id.listView12);
			list.setAdapter(adapter);
			
			while((line=bufferder.readLine())!=null){
				
				page+=line;
			}
			try {
				JSONArray jArr = new JSONArray(page);
				String result ="";
				String result1 ="";
				String result2 ="";
				String result3 ="";
				for (int i = 0; i < jArr.length(); i++) {
				JSONObject json = jArr.getJSONObject(i);
				result = json.getJSONObject("Board").getString("id");
				String result_user_no = String.format("%03d", Integer.parseInt(result));
				result1 = json.getJSONObject("Board").getString("title");
				result2 = json.getJSONObject("Board").getString("content");
				result3 = json.getJSONObject("Board").getString("date");
			
				arr.add(new BoardDTO(result_user_no, result1, result2, result3));
				//result1 += jArr.getString("id");
				//id = json.getString("id");
				//title = json. getString("title");
				//String content = json.getString("content");
				//String date = json.getString("date");
			}
			adapter.notifyDataSetChanged();
			//txt1.setText(result3);
			//String data = "받은 데이터 : " + id + title;
			//txt1.setText(data);
			
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.select, menu);
		return true;
	}

}
